package com.atguigu.gulimall.seckill.schedule;

import com.atguigu.gulimall.seckill.service.SeckillService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class SeckillScheduled {
    @Autowired
    private SeckillService seckillService;
    @Autowired
    private RedissonClient redissonClient;
    private final String SKUKILL_LOCK="seckill:lock";
    /**
     * 每天晚上3点定时
     * 上架最近三天的秒杀商品
     */
    @Scheduled(cron = "0 0 3 * * *")
    @Async
    public void scheduleUploadLatest3(){
        RLock lock = redissonClient.getLock(SKUKILL_LOCK);
        try {
            lock.lock(10, TimeUnit.SECONDS);
            seckillService.uploadloadLates3();
            System.out.println("商品上架了");
        }finally {
            lock.unlock();
        }

    }
}
